/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.mBean;

import com.aptech.entities.TblElevation;
import com.aptech.entities.TblElevationType;
import com.aptech.mBean.util.JsfUtil;
import com.aptech.mBean.util.PaginationHelper;
import com.aptech.sBean.TblElevationFacade;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Winner
 */
@ManagedBean(name = "elevationController")
@SessionScoped
public class ElevationController implements Serializable {

    @EJB
    private TblElevationFacade tblElevationFacade;
    private DataModel items = null;
    private PaginationHelper paginationHelper;
    private TblElevation current;
    private int selectedItemIndex;
    private TblElevationType tblElevationType;

    public TblElevationType getTblElevationType() {
        return tblElevationType;
    }

    public void setTblElevationType(TblElevationType tblElevationType) {
        this.tblElevationType = tblElevationType;
    }

    /**
     * Creates a new instance of ElevationController
     */
    public ElevationController() {
    }
    //thằng này để lấy ra current

    public TblElevation getSelected() {
        if (current == null) {
            current = new TblElevation();
            selectedItemIndex = -1;
        }
        return current;
    }

    private TblElevationFacade getFacade() {
        return tblElevationFacade;
    }
    // thằng này chưa tìm hiểu, vì ko biết nó có cần thiết ko

    public PaginationHelper getPaginationHelper() {
        if (paginationHelper == null) {
            paginationHelper = new PaginationHelper(10) {
                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return paginationHelper;
    }

    // Thằng này là model chứa data của jsf này. nó như bộ nhớ trung gian. nên lưu í khi dùng phải xóa dữ liệu cũ hợp lý
    public DataModel getItems() {
        if (items == null) {
            items = getPaginationHelper().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    private void recreatePagination() {
        paginationHelper = null;
    }

//    public String prepareView() {
//        recreateModel();
//        recreatePagination();
//        return "elevationManager";
//    }
    public String prepareList() {
        recreateModel();
        return "ElevationManagement";
    }

    public void onEdit(RowEditEvent e) {
        FacesMessage msg = new FacesMessage("Edited Elevation", ((TblElevation) e.getObject()).getElName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        getFacade().edit((TblElevation) e.getObject());
    }

    public void onEditCancel(RowEditEvent e) {
        FacesMessage msg = new FacesMessage("Edit Elevation Cancelled", ((TblElevation) e.getObject()).getElName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(tblElevationFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(tblElevationFacade.findAll(), true);
    }

    public List<TblElevation> getTblElevationByEtID() {
        if (tblElevationType != null) {
            List<TblElevation> list = (List<TblElevation>) getFacade().getTblElevationByEtID(tblElevationType.getEtID());
            return list;
        }
        return null;
    }

    public String prepareCreate() {
        current = new TblElevation();
        selectedItemIndex = -1;
        return "Create";
    }

    public String create() {
        try {
            getFacade().create(current);
            return prepareCreate();
        } catch (Exception e) {
            return null;
        }
    }
    // Thằng này để convert các biến sang entity này

    @FacesConverter(forClass = TblElevation.class)
    public static class ElevationControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            ElevationController controller = (ElevationController) context.getApplication().getELResolver().
                    getValue(context.getELContext(), null, "elevationController");
            return controller.tblElevationFacade.find(getKey(value));
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof TblElevation) {
                TblElevation o = (TblElevation) object;
                return getStringKey(o.getElID());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + TblElevation.class.getName());
            }
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }
    }
}
